home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Temático 40 Febrero 2004.iso / DOS / ntfs / user / nttools.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-02-11  |  4.5 KB  |  217 lines

  1. /*
  2.  *  nttools.c
  3.  *  Helper functions for the tools
  4.  *
  5.  *  Copyright (C) 1995-1997, 1999 Martin von L÷wis
  6.  *  Copyright (C) 1997 RΘgis Duchesne
  7.  */
  8.  
  9. #ifdef HAVE_CONFIG_H
  10. #include "config.h"
  11. #endif
  12.  
  13. #include "ntfstypes.h"
  14. #include "struct.h"
  15. #include "nttools.h"
  16.  
  17. #include <stdio.h>
  18. #ifdef HAVE_FCNTL_H
  19. #include <fcntl.h>
  20. #endif
  21. #ifdef HAVE_IO_H
  22. #include <io.h>
  23. #endif
  24. #include <errno.h>
  25. #ifdef HAVE_UNISTD_H
  26. #include <unistd.h>
  27. #endif
  28. #include <sys/stat.h>
  29. #include <sys/types.h>
  30. #include <stdarg.h>
  31. #include <time.h>
  32. #include <ctype.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include "super.h"
  36. #include "inode.h"
  37. #include "dir.h"
  38. #include "macros.h"
  39. #include "support.h"
  40. #include "util.h"
  41. #ifdef MSDOS
  42. #include "../dos/readdisk.h"
  43. #endif
  44.  
  45. ntfs_size_t ntfs_lseek(int fd, ntfs_size_t offset, int whence)
  46. {
  47. #ifdef __linux__
  48.     /* Linux should have llseek, although glibc does not have a prototype */
  49.     loff_t llseek(int,loff_t, int);
  50.     return llseek(fd,(loff_t)offset,whence);
  51. #elif defined MSDOS
  52.     return lseek_dev(fd,(off_t)offset,whence);
  53. #else
  54.     return lseek(fd,(off_t)offset,whence);
  55. #endif
  56. }
  57.  
  58. void ntfs_release_cluster(void *data)
  59. {
  60.     free(data);
  61. }
  62.  
  63. int open_volume(char *name)
  64. {
  65.     /* binary for win32 support */
  66. #ifndef O_BINARY
  67. #define O_BINARY 0
  68. #endif
  69. #ifdef MSDOS
  70.     int fd=open_dev(name,O_RDWR|O_BINARY);
  71. #else
  72.     int fd=open(name,O_RDWR|O_BINARY);
  73. #endif
  74.     if(fd==-1 && errno==EACCES){
  75.         fprintf(stderr,"RW denied, trying RO\n");
  76. #ifdef MSDOS
  77.         fd=open_dev(name,O_RDONLY);
  78. #else
  79.         fd=open(name,O_RDONLY);
  80. #endif
  81.     }
  82.     if(fd==-1)
  83.     {
  84.         perror("open");
  85.         exit(0);
  86.     }
  87.     return fd;
  88. }
  89.  
  90. ntfs_volume *the_vol;
  91.  
  92. ntfs_volume *ntfs_open_volume(char *file, int bias, int silent, int no_inodes)
  93. {
  94.     int fd;
  95.     char cluster0[512];
  96.     ntfs_io io;
  97.     ntfs_volume *newvol;
  98.     char *debug;
  99.  
  100.     /* Set debugging level. */
  101.     debug = getenv ("NTFSDEBUG");
  102.     if (debug) {
  103.         extern int ntdebug;
  104.         ntdebug = atoi (debug);
  105.     }
  106.  
  107.     io.fn_put=0;
  108.     io.fn_get=0; /* we don't need copy functions here */
  109.     newvol=malloc(sizeof(ntfs_volume));
  110.     newvol->partition_bias=bias;
  111. #ifdef NTFS_VOLUME
  112.     NTFS_FD(newvol)=fd=open_volume(file=file?file:NTFS_VOLUME);
  113. #else
  114.     if(!file){
  115.         fprintf(stderr,"No volume specified or configured\n");
  116.         return 0;
  117.     }
  118.     NTFS_FD(newvol)=fd=open_volume(file);
  119. #endif
  120.     if(fd<0)
  121.     {       
  122.         return 0;
  123.     }
  124.     /* read the boot sector */
  125.     io.do_read=1;
  126.     io.param=cluster0;
  127.     io.size=512;
  128.     ntfs_getput_clusters(newvol,0,0,&io);
  129.     if(!IS_NTFS_VOLUME(cluster0)){
  130.         fprintf(stderr,"Not a NTFS volume:%s\n",file);
  131.         the_vol = newvol;
  132.         the_vol->clustersize = 512;
  133.         return 0;
  134.     }
  135.     ntfs_init_volume(newvol,cluster0);
  136.     /* read the first mft record */
  137.     newvol->mft=malloc(newvol->mft_recordsize);
  138.     io.do_read=1;
  139.     io.param=newvol->mft;
  140.     io.size=newvol->mft_recordsize;
  141.     ntfs_getput_clusters(newvol,newvol->mft_cluster,0,&io);
  142.     /* fix it */
  143.     if(!ntfs_check_mft_record(newvol,newvol->mft)){
  144.         fprintf(stderr,"MFT record not at cluster 0x%X\n",newvol->mft_cluster);
  145.         return 0;
  146.     }
  147.     if(!silent)
  148.         fprintf(stderr,"MFT record at block 0x%X, offset 0x%X\n",newvol->mft_cluster,
  149.             newvol->mft_cluster*newvol->clustersize);
  150.     if(!no_inodes)
  151.         ntfs_load_special_files(newvol);
  152.     return newvol;
  153. }
  154.  
  155. /* print a unicode string */
  156. void uniprint(char *first, int length)
  157. {
  158.     while(length--){
  159.         putchar(*first++);
  160.         /* character above 255 or not a valid unicode string */
  161.         if(*first++){
  162.             printf("!!!!Error printing file name\n");
  163.             return;
  164.         }
  165.     }
  166. }
  167.  
  168. void uniprint_lower(char *first,int length)
  169. {
  170.     while(length--){
  171.         putchar(tolower(*first));
  172.         first++;
  173.         /* character above 255 or not a valid unicode string */
  174.         if(*first++){
  175.             printf("!!!!Error printing file name\n");
  176.             return;
  177.         }
  178.     }
  179. }
  180.  
  181. /* unit of 100ns since 1.1.1601 */
  182. void print_time(ntfs_time64_t t)
  183. {
  184.     ntfs_time64_t sec;
  185.     ntfs_time_t unix_utc;
  186.     char *str;
  187.     sec=t/10000000;
  188.     unix_utc=sec-((ntfs_time64_t)369*365+89)*24*3600;
  189.     str=ctime(&unix_utc);
  190.     /* remove \n */
  191.     str[strlen(str)-1]='\0';
  192.     printf("%s",str);
  193. }
  194.  
  195. /* answer the MFT record number for file name in directory ino */
  196. int ntfs_find_file(ntfs_inode *ino, char *name)
  197. {
  198.     char item[1000];
  199.     ntfs_iterate_s walk;
  200.     int error=ntfs_decodeuni(ino->vol,name,strlen(name),
  201.                  &walk.name,&walk.namelen);
  202.     if(error)
  203.         return -1;
  204.     walk.type=BY_NAME;
  205.     walk.dir=ino;
  206.     walk.result=item;
  207.     if(!ntfs_getdir_byname(&walk))return -1;
  208.     free(walk.name);
  209.     return *(int*)item;
  210. }
  211.  
  212. /*
  213.  * Local variables:
  214.  * c-file-style: "linux"
  215.  * End:
  216.  */
  217.